package com.hcrazy.manchester.app.services;

import java.util.ArrayList;
import java.util.List;

import com.hcrazy.manchester.app.jdbc.LiveAddressDAO;
import com.hcrazy.manchester.app.jdbc.LiveBallDAO;
import com.hcrazy.manchester.app.jdbc.LiveCommonDAO;
import com.hcrazy.manchester.app.jdbc.LiveSimpleDAO;
import com.hcrazy.manchester.app.jdbc.LiveSortDAO;
import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.LiveCommon;
import com.hcrazy.manchester.app.pojo.LiveSort;
import com.hcrazy.manchester.app.utils.Constant;

public class LiveSortService {

	private LiveSortDAO liveSortDAO;
	
	private LiveAddressDAO liveAddressDAO;
	
	private LiveBallDAO liveBallDAO;
	
	private LiveCommonDAO liveCommonDAO;
	
	private LiveSimpleDAO liveSimpleDAO;
	
	public LiveAddressDAO getLiveAddressDAO() {
		return liveAddressDAO;
	}

	public void setLiveAddressDAO(LiveAddressDAO liveAddressDAO) {
		this.liveAddressDAO = liveAddressDAO;
	}

	public LiveBallDAO getLiveBallDAO() {
		return liveBallDAO;
	}

	public void setLiveBallDAO(LiveBallDAO liveBallDAO) {
		this.liveBallDAO = liveBallDAO;
	}

	public LiveCommonDAO getLiveCommonDAO() {
		return liveCommonDAO;
	}

	public void setLiveCommonDAO(LiveCommonDAO liveCommonDAO) {
		this.liveCommonDAO = liveCommonDAO;
	}

	public LiveSimpleDAO getLiveSimpleDAO() {
		return liveSimpleDAO;
	}

	public void setLiveSimpleDAO(LiveSimpleDAO liveSimpleDAO) {
		this.liveSimpleDAO = liveSimpleDAO;
	}

	public LiveSortDAO getLiveSortDAO() {
		return liveSortDAO;
	}

	public void setLiveSortDAO(LiveSortDAO liveSortDAO) {
		this.liveSortDAO = liveSortDAO;
	}

	public ErrorMsg addOrUpdateLiveSort(LiveSort sort) throws Exception{
		ErrorMsg msg = new ErrorMsg();
		msg.setCode(Constant.SUCCESS);//success
		
		if(sort.getSortId()>0){
			//编辑分类时，如果存在子分类，则将funCod强置为0
			List<LiveSort> children = liveSortDAO.getByParentId(sort.getSortId());
			if(null != children && !children.isEmpty()){
				sort.setFunCode(0);
			}
			liveSortDAO.updateLiveSort(sort);
		}else{
			//新增分类时，如果存在父分类，则将父分类的funCode置为0
			if(sort.getSortParent()>0){
				LiveSort parent = liveSortDAO.getById(sort.getSortParent());
				//更新父分类，设置父分类的funCode=0
				if(null != parent){
					parent.setFunCode(0);
					liveSortDAO.updateLiveSort(parent);
				}
				//获取父分类的sortCord
				String parentSortCode = parent.getSortCode();
				sort.setSortCode(parentSortCode + parent.getSortId() + ":");
			}else{
				sort.setSortCode(":0:");
			}
			liveSortDAO.addLiveSort(sort);
		}
		
		return msg;
	}
	
	/**
	 * 获取二层的树状结构
	 * @param type
	 * @return
	 * @throws Exception
	 */
	public List<LiveSort> getTwoLayerSorts(int type) throws Exception{
		List<LiveSort> list = liveSortDAO.listAllSorts(type);
		LiveSort root = new LiveSort();
		root.setSortId(0);
		for(LiveSort sort:list){
			if(sort.getSortParent() == root.getSortId()){
				for(LiveSort sort1:list){
					if(sort1.getSortParent() == sort.getSortId()){
						sort.getChildren().add(sort1);
					}
				}
				root.getChildren().add(sort);
			}
		}
		
		return root.getChildren();
	}
	
	public List<LiveSort> getAllSorts(int type,int parentId) throws Exception{
		List<LiveSort> list = liveSortDAO.listAllSorts(type);
		LiveSort root = new LiveSort();
		root.setSortId(parentId);
		setChildren(root, list);
		
		return root.getChildren();
	}
	
	private void setChildren(LiveSort root,List<LiveSort> list){
		for(LiveSort sort:list){
			if(sort.getSortParent() == root.getSortId()){
				setChildren(sort, list);
				root.getChildren().add(sort);
			}
		}
	}
	
	public LiveSort getSortById(int sortId) throws Exception{
		return liveSortDAO.getById(sortId);
	}
	
	public String delLiveSort(int sortId) throws Exception{
		
		//判断，如果该分类下有信息，则提示先删除信息再来删除分类
		if(!liveAddressDAO.getAllInfosBySortId(sortId).isEmpty()){
			return "该分类下存在地址类信息内容，请先删除相关信息！";
		}
		if(!liveBallDAO.getAllInfosBySortId(sortId).isEmpty()){
			return "该分类下存在球赛类信息内容，请先删除相关信息！";
		}
		if(!liveCommonDAO.getAllInfosBySortId(sortId).isEmpty()){
			return "该分类下存在常用类信息内容，请先删除相关信息！";
		}
		if(!liveSimpleDAO.getAllInfosBySortId(sortId).isEmpty()){
			return "该分类下存在综合类信息内容，请先删除相关信息！";
		}
		
		//删除子分类
		liveSortDAO.delLiveSortByParentId(sortId);
		
		//删除本分类
		liveSortDAO.delLiveSort(sortId);
		
		//清空所有父分类不存在的分类
		
		return null;
	}
	
	/**
	 * 根据功能编号查找适合的类别
	 * 去掉有子分类的类别
	 * @param funCode
	 * @return
	 * @throws Exception
	 */
	public List<LiveSort> getLiveSortsByFunCode(int funCode) throws Exception{
		List<LiveSort> list = liveSortDAO.getSortByFunCode(funCode);
		List<LiveSort> newList = new ArrayList<LiveSort>();
		for(LiveSort sort:list){
			int sortId = sort.getSortId();
			//判断是否存在子分类
			if(!liveSortDAO.hasChildren(sortId)){
				//判断是否有父分类
				if(sort.getSortParent()>0){
					//获取父分类
					LiveSort parent = liveSortDAO.getById(sort.getSortParent());
					if(null == parent){
						continue;
					}
					sort.setSortName(parent.getSortName()+" : "+sort.getSortName());
				}
				newList.add(sort);
			}
			
		}
		
		return newList;
	}
	
	public Object getInfoDetails(int infoId,int sortId) throws Exception {
		LiveSort sort = liveSortDAO.getById(sortId);
		if(null != sort){
			//根据funCode获取不同信息内容
			int funCode = sort.getFunCode();
			switch(funCode){
				case Constant.FUN_CODE_ADDRESS:
					Object o = liveAddressDAO.getById(infoId,true).getDetails();
					return o;
				case Constant.FUN_CODE_BALL:
					return null;
				case Constant.FUN_CODE_COMMON:
					return null;
				case Constant.FUN_CODE_SIMPLE:
					return liveSimpleDAO.getById(infoId,true).getDetails();
				default:
					return null;
				
			}
		}
		return null;
	}
	
	public Object getInfoById(int infoId,int sortId,boolean hasDetail) throws Exception {
		LiveSort sort = liveSortDAO.getById(sortId);
		if(null != sort){
			//根据funCode获取不同信息内容
			int funCode = sort.getFunCode();
			switch(funCode){
				case Constant.FUN_CODE_ADDRESS:
					return liveAddressDAO.getById(infoId,hasDetail);
				case Constant.FUN_CODE_BALL:
					return liveBallDAO.getById(infoId);
				case Constant.FUN_CODE_COMMON:
					return liveCommonDAO.getById(infoId);
				case Constant.FUN_CODE_SIMPLE:
					return liveSimpleDAO.getById(infoId,hasDetail);
				default:
					return null;
				
			}
		}
		return null;
	}
	
	public LiveSort getLiveSortWithInfos(int sortId,boolean hasDetail) throws Exception{
		LiveSort sort = liveSortDAO.getById(sortId);
		if(null != sort){
			//根据funCode获取不同信息内容
			int funCode = sort.getFunCode();
			switch(funCode){
				case Constant.FUN_CODE_ADDRESS:
					if(hasDetail){
						sort.getInfos().addAll(liveAddressDAO.getAllInfosHasDetailBySortId(sortId));
					}else{
						sort.getInfos().addAll(liveAddressDAO.getAllInfosBySortId(sortId));
					}
					
					break;
				case Constant.FUN_CODE_BALL:
					sort.getInfos().addAll(liveBallDAO.getAllInfosBySortId(sortId));
					break;
				case Constant.FUN_CODE_COMMON:
					sort.getInfos().addAll(liveCommonDAO.getAllInfosBySortId(sortId));
					break;
				case Constant.FUN_CODE_SIMPLE:
					if(hasDetail){
						sort.getInfos().addAll(liveSimpleDAO.getAllInfosHasDetailsBySortId(sortId));
					}else{
						sort.getInfos().addAll(liveSimpleDAO.getAllInfosBySortId(sortId));
					}
					
					break;
				
			}
		}
		
		return sort;
	}
	
	public List<LiveCommon> getCommonInfos(int sortId) throws Exception{
		List<LiveCommon> list = null;
		if(sortId > 0){
			list = liveCommonDAO.getAllInfosBySortId(sortId);
		}else{
			list = liveCommonDAO.getAllInfos();
		}
		
		return list;
	}
}
